﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace WingbotDB
{
    class Program
    {
        static void Main(string[] args)
        {
            var sqlstring = new SqlConnectionStringBuilder
            {
                DataSource = @"(local)\SQLExpress",
                InitialCatalog = "wingbot_db",
                IntegratedSecurity = true
            };
            var connection = new SqlConnection(sqlstring.ConnectionString);
            try
            {
                connection.Open();
                string table_name = "Reflecters";
                var adapter = new SqlDataAdapter("select * from " + table_name, connection);
                var dataset = new DataSet();
                adapter.Fill(dataset, table_name);
                var table = dataset.Tables[table_name];
                foreach (DataRow row in table.Rows)
                {
                    foreach (DataColumn column in table.Columns)
                    {
                        Console.Write($"|{row[column]}|"); //遍历表中的每个单元格
                    }
                    Console.Write("\r\n");
                }

                // 修改一行
                table.Rows[0]["x"] = Convert.ToDouble(table.Rows[0]["x"]) + 0.1;
                /*
                 * 对DataSet的操作（更改、增加、删除）仅是在本地修改，
                 * 若要提交到真实数据库中则需要SqlCommandBuilder对象。
                 */
                var cmdbuilder = new SqlCommandBuilder(adapter); //为adapter赋予SqlCommandBuilder功能
                var newrow = table.NewRow();
                newrow["id"] = table.Rows.Count + 1;
                newrow["x"] = 0.01;
                newrow["y"] = 0.02;
                table.Rows.Add(newrow);
                adapter.Update(dataset, table_name);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
            connection.Close();
        }
    }
}
